<?php
namespace control;
/*
*部门类
*/
class contactStaffClass extends adIndexClass
{
    private const page  = 'contactStaff'; # 权限
    private const table = 'staff'; # 数据表
    private const title = '员工通讯录';

    /*职位入口页*/
    public function home()
    {
        $search = '<i class="container-left-search-ico"></i>'.text(["name"=> 'name', 'title'=>L('person.table.sou_suo_bu_men'), 'class'=> 'container-left-search', 'keyup'=> 'containerLeftSearch,job']);
        $space = space;
        $option['click'] = 'replaceRight(this, \'contactStaff\')';
        $all = departmentClass::getDepartmentAll($option);

        //返回
        $rightHtml = $this->homeRight();
        $html =<<<EOT
        <div class="container-main">
            <div class="container-left left" id="container-left">
                {$search}
                {$space}
                {$all}
            </div>
            <div class="container-right left" id="container-detail">
				{$rightHtml}
            </div>
        </div>
EOT;
        //返回
        return $this->homeFun($html);
    }
    public function homeRight()
    {
        // 搜索
        $html =
            text(["name"=> 'jobName', 'title'=>L('person.table.zwmc'), 'class'=> 'text',])
            .text(["name"=> 'name', 'title'=>L('person.table.xing_ming'), 'class'=> 'text',])
            .text(["name"=> 'tel', 'title'=>L('person.table.dian_hua'), 'class'=> 'text',])
            .hidden(['name'=>'objId', 'value'=>$this->post('objId') ]);
        return $this->search($html);
    }

    /**
     * 异步加载
     *
     */
    public function homeRightJson()
    {
        $json['html'] = $this->homeRight();
        return json_encode($json, 320);
    }
    public function homeRightJsonSearch()
    {
        return $this->homeSearch();
    }

    /*职位列表*/
    public function homeSearch()
    {
        //like查询数组
        $like = [
            "name"    => $this->post('name'),
            "tel"     => $this->post('tel'),
        ];
        //全等查询数组
        $equal = [

        ];
        $mWhere = search([
            'like'=> [
                'name' => $this->post('jobName')
            ],
            '='=>[
                'departmentId' => $this->post('objId')
            ]
        ]);
        $and = "workFlow='已复核'";
        if ($mWhere) {
            $and .= " AND `jobId` IN ( SELECT `id` FROM `job` WHERE 1=1 $mWhere ) ";
        }
        //获取查询结果
        $where = $and.search(["like" => $like,"=" => $equal]);
        $page = $this->page(["table" => "staff", "where" => $where, "order" => "time"]);
        $_SESSION[self::page]['sql'] = $page['sql'];
        //获取表头
        $checkAll = $this->checkAll("listForm", "stid");
        $tr[] = [$checkAll, L('person.table.zhi_wei'), L('person.table.xing_ming'), L('person.table.shou_ji_hao')];
        //获取记录
        foreach ($page['array'] as $array) {
            $job = query('job', " id = '{$array['jobId']}' ");
            //列表控件
            $dominate  = [
                "box" => ["name" => "stid","value" => $array['stid']],
            ];
            //本列
            $tr[] = [$this->dominate($dominate), $job['name'], $array['name'], $array['tel']];
        }
        //控件
        $button = "";
        if ($this->power(self::page, "export")) {
            $button .= "<a href='" . root . "control/adIndex.html?cla=".$this->get('cla')."&fun=exportData' class='spanButton'>".L('job.button.export')."</a>\n";
        }
        //返回
        $json['html'] = $page['hint'].$button."<form name='listForm'>".space.$this->tableMany(["tr" => $tr])."</form>".$page['button'].clear;
        return json_encode($json, 320);
    }


    /**
     * 导出
     */
    public function exportData()
    {
        if (!$this->power(self::page, 'export')) {
            $_SESSION['warn'] = L('person.table.nhmzdcdzx');
        } else {
            $data=[];
            $sql   = $_SESSION[self::page]['sql'];
            $title = [L('person.table.zhi_wei'), L('person.table.xing_ming'), L('person.table.shou_ji_hao')];
            $all   = $this->sqlFind($sql);
            //获取记录
            if (!$all) {
                $_SESSION['warn'] = L('person.table.zan_wu_ji_lu');
            } else {
                foreach ($all as $k => $array) {
                    // 子查询
                    $job = query('job', " id = '{$array['jobId']}' ");
                    // 数据
                    $data[] =  [$job['name'], $array['name'], $array['tel']];
                }
                $text =str_replace(['{{staffName}}'],[$this->control['name']],L('person.table.staffExport'));
                $this->logText(["type" => self::page, "text" => $text]);
                $this->excelOut($title, $data, L('person.table.staffBook').'.xlsx');
                exit(0);
            }
        }
        $locationUrl = root . 'control/adIndex.html?cla='.$this->get('cla').'&fun=home';
        header("Location:" . $locationUrl);
        exit(0);
    }
}
